package com.etc.zzsys.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.zzsys.entity.Answer;
import com.etc.zzsys.service.AnswerService;
import com.etc.zzsys.service.impl.AnswerServiceImpl;
import com.etc.zzsys.util.PageData;

/**
 * Servlet implementation class AnswerServlet
 */
@WebServlet("/as.do")
public class AnswerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AnswerServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String op = request.getParameter("op");
		if ("query".equals(op)) {
			doQueryByPage(request, response);
		}
		if("search".equals(op)) {
			doSearch(request, response);
		}
		
		if("queryPage".equals(op)) {
			doQueryByPage(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	protected void doQuery(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		AnswerService as = new AnswerServiceImpl();

		List<Answer> list = as.getAnswer();
		request.setAttribute("list", list);
		request.getRequestDispatcher("answer.jsp").forward(request, response);
	}

	protected void doSearch(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		AnswerService as = new AnswerServiceImpl();

		String key=request.getParameter("key");
		List<Answer> list = as.getAnswerByLike(key);
		request.setAttribute("list", list);
		request.getRequestDispatcher("answer.jsp").forward(request, response);
	}
	protected void doQueryByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		AnswerService as = new AnswerServiceImpl();
		
		int page=1;
		int pageSize=3;
		if(request.getParameter("page")!=null) {
			page=Integer.valueOf(request.getParameter("page"));
		}
		if(request.getParameter("pageSize")!=null) {
			pageSize=Integer.valueOf(request.getParameter("pageSize"));
		}
		String key="";
		if(request.getParameter("key")!=null) {
			key=request.getParameter("key");
		}
		request.setAttribute("key",key);
		PageData<Answer> list=as.queryAnswerByPage(page, pageSize,key);
		
		request.setAttribute("list",list.getData());
		request.setAttribute("pdata", list);
		request.getRequestDispatcher("answer.jsp").forward(request, response);
		
	}
}
